//
//  FMTableTool.m
//  面向对象的数据库操作
//
//  Created by appbanana on 2017/3/18.
//  Copyright © 2017年 appbanana. All rights reserved.
//

#import "FMTableTool.h"
#import "FMSqliteTool.h"

@implementation FMTableTool

+ (BOOL)isTableExists:(NSString *)tableName uid:(NSString *)uid{
    
    NSString *sql = @"select name from sqlite_master";
    NSArray <NSDictionary *>*resultArray = [FMSqliteTool querySql:sql uid:uid];
   __block BOOL isTableExists = NO;
    [resultArray enumerateObjectsUsingBlock:^(NSDictionary * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([[obj[@"name"] lowercaseString] isEqualToString:[tableName lowercaseString]]) {
            isTableExists = YES;
        }
    }];
    return isTableExists;
}


+ (NSArray *)sortedColumnName:(NSString *)tableName uid:(NSString *)uid{
    NSString *sql = [NSString stringWithFormat:@"select sql from sqlite_master where name = '%@'", tableName];
    NSArray *resultArray = [FMSqliteTool querySql:sql uid:uid];
    NSString *sqliteTypeStr = resultArray.firstObject[@"sql"];
    NSString *string = [sqliteTypeStr componentsSeparatedByString:@"("][1];
    NSArray *columnArr = [string componentsSeparatedByString:@","];
    NSMutableArray *array = [NSMutableArray arrayWithCapacity:1];
    for (NSString *str in columnArr) {
        if ([str containsString:@"primary"]) {
            continue ;
        }
        [array addObject:[str componentsSeparatedByString:@" "].firstObject];
    }
    [array sortUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
        return [obj1 compare:obj2];
    }];
    return array;
}




@end
